/**
 * 创建完美正方形布局，专门用于处理3张4000*6000 + 3张6000*4000的图片
 * @param landscapeImages 横向图片数组
 * @param portraitImages 纵向图片数组
 * @param spacing 图片间距
 * @returns 优化后的布局
 */
export function createPerfectSquareLayout(
  landscapeImages: Array<{ id: string; name: string; width: number; height: number; format: string }>,
  portraitImages: Array<{ id: string; name: string; width: number; height: number; format: string }>,
  spacing: number
): { positions: Array<{ id: string; name: string; x: number; y: number; width: number; height: number; format: string }>; width: number; height: number } {
  const positions: Array<{ id: string; name: string; x: number; y: number; width: number; height: number; format: string }> = [];
  
  // 对于3张4000*6000 + 3张6000*4000的图片，我们可以创建一个完美的12000*12000正方形布局
  // 布局如下（优化后，确保12000x12000）：
  // +-------------+-------------+
  // |             |             |
  // |  Landscape1 |  Landscape2 |
  // |  6000x4000  |  6000x4000  |
  // |             |             |
  // +-------------+-------------+
  // |             |             |
  // |  Landscape3 |  Portrait1  |
  // |  6000x4000  |  6000x4000  |
  // |             |  (rotated)  |
  // +-------------+-------------+
  // |             |             |
  // |  Portrait2  |  Portrait3  |
  // |  6000x4000  |  6000x4000  |
  // |  (rotated)  |  (rotated)  |
  // +-------------+-------------+
  
  // 获取典型图片尺寸
  const landscapeWidth = landscapeImages[0].width;  // 6000
  const landscapeHeight = landscapeImages[0].height; // 4000
  const portraitWidth = portraitImages[0].width;    // 4000
  const portraitHeight = portraitImages[0].height;  // 6000
  
  // 第一行：两张横向图片并排
  positions.push({
    id: landscapeImages[0].id,
    name: landscapeImages[0].name,
    x: 0,
    y: 0,
    width: landscapeWidth,
    height: landscapeHeight,
    format: landscapeImages[0].format
  });
  
  positions.push({
    id: landscapeImages[1].id,
    name: landscapeImages[1].name,
    x: landscapeWidth,
    y: 0,
    width: landscapeWidth,
    height: landscapeHeight,
    format: landscapeImages[1].format
  });
  
  // 第二行：一张横向图片和一张旋转后的纵向图片
  positions.push({
    id: landscapeImages[2].id,
    name: landscapeImages[2].name,
    x: 0,
    y: landscapeHeight,
    width: landscapeWidth,
    height: landscapeHeight,
    format: landscapeImages[2].format
  });
  
  // 第二行右侧：第一张纵向图片（旋转90度）
  positions.push({
    id: portraitImages[0].id,
    name: portraitImages[0].name,
    x: landscapeWidth,
    y: landscapeHeight,
    width: portraitHeight, // 旋转后宽高交换
    height: portraitWidth,
    format: portraitImages[0].format
  });
  
  // 第三行：两张旋转后的纵向图片
  positions.push({
    id: portraitImages[1].id,
    name: portraitImages[1].name,
    x: 0,
    y: landscapeHeight * 2,
    width: portraitHeight, // 旋转后宽高交换
    height: portraitWidth,
    format: portraitImages[1].format
  });
  
  positions.push({
    id: portraitImages[2].id,
    name: portraitImages[2].name,
    x: landscapeWidth,
    y: landscapeHeight * 2,
    width: portraitHeight, // 旋转后宽高交换
    height: portraitWidth,
    format: portraitImages[2].format
  });
  
  // 计算布局的宽度和高度
  const width = Math.max(...positions.map(p => p.x + p.width));
  const height = Math.max(...positions.map(p => p.y + p.height));
  
  return { positions, width, height };
}